﻿using System;
using System.Linq;
using Infrastructure.Persistence;
using SocialFront.Core.Domain;

namespace SocialFront.Core.Queries.FooQueries
{
    public class FooWildcardQuery : Query<Foo, Foo>
    {
        public FooWildcardQuery(IReadOnlyRepository repository) 
            : base(repository)
        {
        }

        protected override void ValidateParameter()
        {
            if (Parameter == null)
                throw new InvalidOperationException("FooWildcardQuery => Parameter cannot be null.");
        }

        protected override string DefaultSortExpression
        {
            get 
            { 
                return "Foo1 DESC"; 
            }
        }

        protected override IQueryable<Foo> CreateQuery()
        {
            var query = Repository.Query<Foo>();

            if (Parameter.Id > 0)
                query = query.Where(x => x.Id == Parameter.Id);

            if (!String.IsNullOrEmpty(Parameter.Foo1))
                query = query.Where(x => x.Foo1.Contains(Parameter.Foo1));
            
            return query;
        }
    }
}

